﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using EzDesk.Utility;
using EzDesk.Core.Model;
using Microsoft.Practices.Unity;
using EzDesk.Utility.Generic; 

namespace EzDesk.EzAppApi.Utility
{
    public static class EzDeskAppApiHelper
    {
        public static bool ContainUser(this IEnumerable<SimpleUser> userList, Guid userId)
        {
            foreach (var user in userList)
            {
                if (user.Id == userId)
                {
                    return true;
                }
            }
            return false;
        }


        public static class MemberStringHelper
        {
            public static string JoinUserId(string originalString, Guid userId)
            {
                if (originalString == null)
                {
                    originalString = string.Empty;
                }

                if (originalString == "everyone")
                {
                    return originalString;
                }

                string resultString = null;

                int splitorIndex = originalString.IndexOf('|');

                if (splitorIndex == 0)
                {
                    // '|' 出现在第一个，源字符串只有group，没有UserId
                    resultString = string.Format("{0}{1}", userId.ToString() + originalString);
                }
                else if (splitorIndex > 0)
                {
                    // '|' 出现在中间，源字符串有UserId
                    resultString = string.Format("{0},{1}", userId.ToString(), originalString);
                }
                else if (splitorIndex < 0)
                {
                    // '|' 不存在，源字符串中没有group数据
                    if (originalString.Length == 0)
                    {
                        // 源字符串为空
                        resultString = userId.ToString();
                    }
                    else
                    {
                        resultString = string.Format("{0},{1}", userId.ToString(), originalString);
                    }
                }
                return resultString;
            }
            public static string JoinGroupId(string originalString, Guid groupId)
            {
                if (originalString == null)
                {
                    originalString = string.Empty;
                }

                if (originalString == "everyone")
                {
                    return originalString;
                }

                string resultString = null;

                int splitorIndex = originalString.IndexOf('|');

                if (splitorIndex >= 0)
                {
                    // '|' 出现在中间，源字符串有UserId
                    // xxx,xxx|zzz,zzz
                    resultString = string.Format("{0},{1}", groupId.ToString(), originalString);
                }
                else if (splitorIndex < 0)
                {
                    // '|' 不存在，源字符串中没有group数据
                    resultString = string.Format("{0}|{1}", originalString, groupId.ToString());
                }
                return resultString;

            }

            public static string GetIdStrdFromIdList(IList<Guid> list)
            {
                var feedIdArray = list.Distinct().Select(p => p.ToString());
                return feedIdArray.Join(",");
            }
        }
        
    }

}
